var qqmapsdk;
//位置SDK
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    weatherData: {
      wea: "狂风暴雨",
      air: "00",
      week: "星期三",
    },
    des: {},
    hours: {},
    date: "9012年11月11日",
    temp: "66",
    temp1: "20℃",
    temp2: "10℃",
    city: "点我打开位置权限",
    image_url: "/images/sunny-bg.png",
    image: {
      '晴': 'sunny',
      '多云': 'cloudy',
      '晴转多云': 'cloudy',
      '阵雨': 'heavyrain',
      '小雨': 'lightrain',
      '雪': 'snow',
      '阴': 'overcast',
      '多云转小雨': 'lightrain',
      '阵雨转晴': 'sunny',
      "多云转阴": 'overcast',
      "阵雨转中雨": 'lightrain',
      "晴转阴": "overcast"
    },
    weatherColorMap: {
      '晴': '#cbeefd',
      '阵雨转晴': '#cbeefd',
      '多云': '#deeef6',
      '晴转多云': '#deeef6',
      '阴': '#c6ced2',
      '小雨': '#bdd5e1',
      '多云转小雨': '#bdd5e1',
      '阵雨': '#c5ccd0',
      '雪': '#aae1fc',
      "多云转阴": '#c6ced2',
      "阵雨转中雨": '#bdd5e1',
      "晴转阴": "#c6ced2"
    }
  },
  onTapLocation() {
    var that = this
    wx.openSetting({
      success(res) {
        wx.getSetting({
          success(res) {
            if (res.authSetting['scope.userLocation']) {
              that.getCityAndWeather()
            } else {
              wx.showModal({
                title: '提示',
                content: '亲，打开位置权限才能获取实时天气信息哦',
                confirmText: "好的",
                confirmColor: "#4fa0e4",
                showCancel: false
              })
            }
          }
        })
      }
    })
  },
  getCityAndWeather() { //获取当前城市
    var that = this
    wx.getLocation({
      success: function(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: res => {
            let city = res.result.address_component.city
            that.setData({
              city: city
            })
            that.getWeatherInfo()
          }
        });
      }
    })
  },
  authorize() { //判断是否授权，已授权直接获取当前城市天气信息，没授权就提醒用户授权
    var that = this
    wx.getSetting({
      success: res => {
        if (!res.authSetting["scope.userLocation"]) {
          wx.authorize({
            scope: "scope.userLocation",
            success(res) {
              that.getCityAndWeather()
              console.log(that.data.city)
            },
            fail(err) {
              wx.showModal({
                title: '提示',
                content: '亲，打开位置权限才能获取实时天气信息哦',
                confirmText: "好的",
                confirmColor: "#4fa0e4",
                showCancel: false
              })
            }
          })
        } else {
          that.getCityAndWeather()
        }
      }
    })
  },
  getWeatherInfo: function() { //获取今天的天气信息
    var that = this
    wx.request({
      url: 'https://www.tianqiapi.com/api/?appid=64837846&appsecret=zuNpEpS7&version=v1',
      data: {
        // city: that.data.city.slice(0, 2)
        city: "成都"
      },
      success(res) {
        console.log("所有天气信息数据", res)
        if (res.statusCode === 200) {
          var datas = res.data.data[0]
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userLocation']) {
                var weather = datas.wea
                for (var i = 2; i < 10; i += 2) {
                  var index = datas.index[i]
                  var des = datas.index.unshift(index)
                }
                for (var a = 0; a < 8; a++) {
                  var day = datas.hours[a].day.slice(3, 6)
                  var wind = datas.hours[a].win_speed.slice(1,3)
                  // console.log(day)
                  datas.hours[a].day = day
                  datas.hours[a].win_speed = wind
                }
                // console.log(des)
                that.setData({
                  weatherData: datas,
                  temp: datas.tem.slice(0, 2),
                  date: datas.date.slice(0, 4) + "年" + datas.date.slice(5, 7) + "月" + datas.date.slice(8, 10) + "日",
                  image_url: "/images/" + that.data.image[weather] + "-bg.png",
                  des: datas.index.slice(0, 5),
                  temp1: datas.tem1,
                  temp2: datas.tem2,
                  hours: datas.hours,
                  day: day
                })
                console.log("今天天气信息数据", that.data.weatherData)
                wx.setNavigationBarColor({
                  frontColor: '#000000',
                  backgroundColor: that.data.weatherColorMap[weather]
                })
              } else {
                that.getCityAndWeather()
              }
            }
          })
        } else {
          console.log("请求数据错误")
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.authorize()
    qqmapsdk = new QQMapWX({
      key: '234BZ-UVRK3-47M3O-YINT2-DVECJ-VJFY7'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.authorize()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    setTimeout(function() {
      that.authorize()
      wx.stopPullDownRefresh()
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})